package com.tjx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjx.entity.Order;
import com.tjx.entity.PackageDetail;
import com.tjx.mapper.OrderMapper;
import com.tjx.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjx.utils.OrderNoGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author tjx
 * @since 2025-10-29
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderNoGenerator orderNoGenerator;




        /**
         * 创建订单服务
         * @param order 订单对象，包含订单信息（如用户ID、套餐ID、总金额等）
         * @return 包含订单ID、订单编号、订单状态等信息的订单对象
         */
        @Override
        public Order createOrder(Order order) {
            if (order == null) {
                throw new IllegalArgumentException("订单对象不能为空");
            }
            // 生成订单编号
            String orderNo = orderNoGenerator.generateOrderNo();
            order.setOrderNo(orderNo);
            // 设置订单状态为待支付
            order.setOrderStatus("pending");
            // 设置订单创建时间
            order.setCreateTime(LocalDateTime.now());
            // 插入订单到数据库
            orderMapper.insert(order);
            return order;
        }

        /**
         * 查询用户所有订单服务
         * @param order 订单对象，包含用户ID
         * @return 包含订单详细信息的订单对象
         */
        @Override
        public List<Order> getOrderByUserId(Order order) {
            if (order == null || order.getUserId() == null) {
                throw new IllegalArgumentException("订单ID不能为空");
            }
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", order.getUserId())
                    .eq("is_deleted", 0);
            List<Order> orders = orderMapper.selectList(queryWrapper);
            return orders;


        }
}
